<?php


/**
 * Controller for example application 1.
 */
class Controller_App1 extends Psa_Plugin_Controller{


	/**
	 * Main controller method.
	 * This method is called by router.
	 * @param array $url_args Action name. Parts of URL path.
	 */
	function psa_main($args){

		
		// global config array
		global $PSA_CFG;
		

		// From controller we need to call models and views.
		// First you need to decide which models and views to call.
		// For that we will make for each request one small 'action' method which will
		// return models and views for this action.
		// You can make that with if-else (switch) statements if you like.
		// You can put this action methods in separate file(s) and include it
		// here. It is up to you how you will do it, but you must somehow
		// get parameters for psa_run_plugins() function.

		// It is good to append some word to your acaction methodlike '_action' for security reasons
		// to be sure that someone is not able to call some built-in PHP function or some user defined
		// functions.

		// See also example application 2 where object with action methods is used which is much better
		// solution.
		

		/**
		 * This is default action (like index action).
		 * This action will be called if no other action is requested.
		 */
		function default_action(){

			// views for this action. We don't need any model for this action in this simple test application
			$return['view']['View_App1']['generate_html'] = array();
			
			return $return;
		}


		/**
		 * Action for even numbers
		 */
		function even_action(){

			// models for this action
			$return['model']['Model_App1']['even_numbers'] = array(100);
			
			// views for this action
			$return['view']['View_App1']['even_numbers'] = array();
			// generate_html() method of View_App1 class will be called in every request in this application
			// so you can put this line outside of the action methods and append this element to
			// returning array from the action methods. I'll live it here in this example.
			$return['view']['View_App1']['generate_html'] = array();
			
			return $return;
		}


		/**
		 * Action for odd numbers
		 */
		function odd_action(){

			// models for this action
			$return['model']['Model_App1']['odd_numbers'] = array();
			
			// views for this action
			$return['view']['View_App1']['odd_numbers'] = array();
			$return['view']['View_App1']['generate_html'] = array();
			return $return;
		}


		/**
		 * Action for prim numbers
		 */
		function prim_action(){

			// models for this action
			$return['model']['Model_App1']['prim_numbers'] = array((int) @$_GET['max_prim']);
			
			// views for this action
			$return['view']['View_App1']['prim_numbers'] = array();
			$return['view']['View_App1']['generate_html'] = array();
			return $return;
		}
		
		
		// put some values in result object to be available in all templates
		Psa_Result::get_instance()->basedir_web = $PSA_CFG['folders']['basedir_web'];
		
		// get action method name
		$action_function = $args[0] . '_action';
		
		// check if we have defined this action and if not set default_action to be called
		if(!function_exists($action_function)){
			$action_function = 'default_action';
		}
		
		// call action method to get parameters for psa_run_plugins() method
		$run_plugins = $action_function();
		
		// first run model plugins
		if(isset($run_plugins['model']))
			psa_run_plugins($run_plugins['model']);
			
		// After model plugins are called assign all results from models (every property in $psa_result object)
		// to Smarty ($psa_smarty) object. I use Smarty template engine here.
		$this->smarty_assign_result();
		
		// run view plugins at the end
		if(isset($run_plugins['view']))
			psa_run_plugins($run_plugins['view']);
	}
}
?>